﻿using Shinva.LADModel.Abstractions;
using Shinva.Variables.Interface;
using Shinva.Variables.Models;
using Shinva.Variables.Type.bit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Shinva.LADModel.Type.MoveOperations
{
    public class Move : ElementNodeBase
    {
        public IPlcMember val { get; set; } //实际是in
        public IPlcMember out1 { get; set; }
        public IPlcMember out2 { get; set; }
        public IPlcMember out3 { get; set; }
        public IPlcMember out4 { get; set; }
        public IPlcMember out5 { get; set; }
        public IPlcMember out6 { get; set; }
        public IPlcMember out7 { get; set; }
        public IPlcMember out8 { get; set; }
        public IPlcMember out9 { get; set; }
        private int _outCount = 1;
        public override PlcBool EN
        {
            get
            {
                return en;
            }
            set
            {
                en = value;
            }
        }
        public override PlcBool ENO
        {
            get
            {
                eno.Value = en.Value && Move_value();
                return eno;
            }
            set
            {
                eno = value;
            }
        }

        public Move(int outCount)
        {
            Name = "Move";
            _outCount = outCount;
            variableTableInterface.AddVariable("val", StorageType.Input);
            variableTableInterface.AddVariable("out1", StorageType.Output);
            variableTableInterface.AddVariable("out2", StorageType.Output);
            variableTableInterface.AddVariable("out3", StorageType.Output);
            variableTableInterface.AddVariable("out4", StorageType.Output);
            variableTableInterface.AddVariable("out5", StorageType.Output);
            variableTableInterface.AddVariable("out6", StorageType.Output);
            variableTableInterface.AddVariable("out7", StorageType.Output);
            variableTableInterface.AddVariable("out8", StorageType.Output);
            variableTableInterface.AddVariable("out9", StorageType.Output);
            val = variableTableInterface.GetVariableValue<IPlcMember>("val");
            out1 = variableTableInterface.GetVariableValue<IPlcMember>("out1");
            out2 = variableTableInterface.GetVariableValue<IPlcMember>("out2");
            out3 = variableTableInterface.GetVariableValue<IPlcMember>("out3");
            out4 = variableTableInterface.GetVariableValue<IPlcMember>("out4");
            out5 = variableTableInterface.GetVariableValue<IPlcMember>("out5");
            out6 = variableTableInterface.GetVariableValue<IPlcMember>("out6");
            out7 = variableTableInterface.GetVariableValue<IPlcMember>("out7");
            out8 = variableTableInterface.GetVariableValue<IPlcMember>("out8");
            out9 = variableTableInterface.GetVariableValue<IPlcMember>("out9");
        }

        private bool Move_value()
        {
            if (en.Value)
            {
                if (_outCount >= 1) { if (out1 == null) return false; out1.Value = val.Value; }
                if (_outCount >= 2) { if (out2 == null) return false; out2.Value = val.Value; }
                if (_outCount >= 3)
                {
                    if (out3 == null)
                        return false;
                    out3.Value = val.Value;
                }
                if (_outCount >= 4)
                {
                    if (out4 == null)
                        return false;
                    out4.Value = val.Value;
                }
                if (_outCount >= 5)
                {
                    if (out5 == null)
                        return false;
                    out5.Value = val.Value;
                }
                if (_outCount >= 6)
                {
                    if (out6 == null)
                        return false;
                    out6.Value = val.Value;
                }
                if (_outCount >= 7)
                {
                    if (out7 == null)
                        return false;
                    out7.Value = val.Value;
                }
                if (_outCount >= 8)
                {
                    if (out8 == null)
                        return false;
                    out8.Value = val.Value;
                }
                if (_outCount >= 9)
                {
                    if (out9 == null)
                        return false;
                    out9.Value = val.Value;
                }
                return true;
            }
            return false;
        }

        public override IPlcMember GetInterfaceVariableByName(string name)
        {
            if (name == "in") return val;
            else return variableTableInterface.GetVariableValue<IPlcMember>(name);
        }
    }
}
